﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class FurnitureMechanismTouch : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Furniture_FurnitureMechanismTouch;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<FurnitureMechanismTouchReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.furnitureMechanismId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：furnitureMechanismId参数为空）" });
				return;
			}
			var furnitureMechanismInfo = session.minecraftSessionInfo.furnitureMechanismList.FirstOrDefault(m => m.FurnitureId == req.furnitureMechanismId);
			if (furnitureMechanismInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：furnitureMechanismInfo 信息不存在）" });
				return;
			}
			var mod_FurnitureInfo = furnitureMechanismInfo.GetMod_FurnitureInfo();
			if (mod_FurnitureInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：mod_FurnitureInfo 家具配置表信息不存在）" });
				return;
			}
			furnitureMechanismInfo.Hp -= mod_FurnitureInfo.Hp_expend_Convert;
			if (furnitureMechanismInfo.Hp < 0)
			{
				furnitureMechanismInfo.Hp = 0;
				session.minecraftSessionInfo.furnitureMechanismList.RemoveAll(m => m.FurnitureId == furnitureMechanismInfo.FurnitureId);
			}
			var resp = new FurnitureMechanismTouchResp
			{
				furnitureMechanismId = furnitureMechanismInfo.FurnitureId,
				remainHp = furnitureMechanismInfo.Hp,
				Att = mod_FurnitureInfo.Att_Convert,
				point3 = furnitureMechanismInfo.point3,
			};
			session.Send(defCommand, req, resp);
		}
	}
}
